package com.wuchao.exception;

import com.wuchao.response.Result;
import com.wuchao.response.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@ControllerAdvice
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result exceptionHandle(Exception e){
       // return Result.error();
        if (e instanceof ArithmeticException){
            log.info("算术异常："+e.getMessage());
            return  Result.error(ResultCode.ARITHMETIC_EXCEPTION);
        }else if(e instanceof ArrayIndexOutOfBoundsException){
            return  Result.error(ResultCode.COMMON_FAIL);
        }else if(e instanceof BusinessException){
            return
                    Result.error().code(((BusinessException) e).getCode()).message(((BusinessException) e).getErrMsg());
            //业务异常。链式操作，可以一直点下去,可以选无参的也可以选有参的

        }else {
            return Result.error();//有重载的
        }
    }
}
